import time
import pandas as pd
from selenium import webdriver


def get_one_page_data(driver, gname_list, price_list, commit_list, shop_list, icons_list):
    # 定位所有的li标签
    li_list = driver.find_elements_by_xpath('//div[@id="J_goodsList"]/ul/li')
    for li in li_list:
        '''
        div[@class="p-name p-name-type-2"]  商品名称  /n
        div[@class="p-price"]   价格
        div[@class="p-commit"]   评价数
        div[@class="p-shop"]   店铺
        div[@class="p-icons"]  标签
        '''
        gname = str(li.find_element_by_xpath('./div/div[@class="p-name p-name-type-2"]').text).replace('\n', '')
        gname_list.append(gname)

        price = str(li.find_element_by_xpath('./div/div[@class="p-price"]').text).replace('\n', '')
        price_list.append(price)

        commit = str(li.find_element_by_xpath('./div/div[@class="p-commit"]').text).replace('\n', '')
        commit_list.append(commit)

        shop = str(li.find_element_by_xpath('./div/div[@class="p-shop"]').text).replace('\n', '')
        shop_list.append(shop)

        icons = str(li.find_element_by_xpath('./div/div[@class="p-icons"]').text).replace('\n', '|')
        icons_list.append(icons)

        print(f'商品:{gname}, 价格:{price}, 评价数:{commit}, 店铺:{shop}, 标签:{icons}')
        print('--------------------------')


def load_data(driver):
    # 将当前页面的滚动条拉取到最底部
    driver.execute_script(
        'window.scrollTo(0,document.body.scrollHeight)'
    )
    time.sleep(3)

    while True:
        try:
            driver.find_element_by_xpath('//div[@id="J_scroll_loading"]/span/a').click()
            print('加载不完全，正在点击加载...')
            time.sleep(0.2)
        except:
            print('没有重新加载。。。。')
            break
        if driver.find_element_by_xpath('//div[@id="J_scroll_loading"]/span').text == '正在加载中，请稍后~~':
            print('数据加载完毕！！')
            break


def get_data(driver, gname_list, price_list, commit_list, shop_list, icons_list):
    load_data(driver)
    time.sleep(1)
    get_one_page_data(driver, gname_list, price_list, commit_list, shop_list, icons_list)


def load_Data2(driver):
    # 点击下一页之后最上面出现的加载
    while True:
        try:
            driver.find_element_by_xpath('//div[@id="J_loading"]/div/span/a').click()
            print('前30条数据加载不完全，正在点击加载...')
            time.sleep(0.2)
        except:
            # print('没有重新加载。。。。')
            break
        # if driver.find_element_by_xpath('//div[@id="J_loading"]/div/span/a').text == '正在加载中，请稍后~~':
        #     print('前30条数据加载完毕！！')
        #     break

    time.sleep(1)
    # 将当前页面的滚动条拉取到最底部
    driver.execute_script(
        'window.scrollTo(0,document.body.scrollHeight)'
    )
    time.sleep(3)

    # 加载后30条数据
    while True:
        try:
            driver.find_element_by_xpath('//div[@id="J_scroll_loading"]/span/a').click()
            print('加载不完全，正在点击加载...')
            time.sleep(0.2)
        except:
            print('没有重新加载。。。。')
            break
        if driver.find_element_by_xpath('//div[@id="J_scroll_loading"]/span').text == '正在加载中，请稍后~~':
            print('所有数据加载完毕！！')
            break


def get_data2(driver, gname_list, price_list, commit_list, shop_list, icons_list):
    load_Data2(driver)
    time.sleep(1)
    get_one_page_data(driver, gname_list, price_list, commit_list, shop_list, icons_list)


def get_all_data(driver, gname_list, price_list, commit_list, shop_list, icons_list):
    # 将第一页放在外面获取
    print('====================正在获取第1页的数据...===============================')
    get_data(driver, gname_list, price_list, commit_list, shop_list, icons_list)
    for i in range(2, 11):
        # 定位到下一页的标签元素
        next_page = driver.find_element_by_xpath('//div[@id="J_bottomPage"]/span[1]/a[last()]')
        info = str(next_page.get_attribute('class'))
        if info == 'pn-next':
            # 点击下一页
            next_page.click()
            print('----已经点击----------')
            time.sleep(2)
            print(f'====================正在获取第{i}页的数据...===============================')
            get_data2(driver, gname_list, price_list, commit_list, shop_list, icons_list)
        else:
            break


def quit_driver(driver):
    # 睡眠n秒
    time.sleep(5)

    # 关闭整个浏览器
    driver.quit()


if __name__ == '__main__':
    '''
    主url: https://www.jd.com/
    '''
    # 1.创建浏览器对象 - 此时浏览器打开
    # driver = webdriver.Chrome()

    options1 = webdriver.ChromeOptions()
    options1.add_experimental_option("excludeSwitches", ['enable-automation'])

    driver = webdriver.Chrome(chrome_options=options1)

    driver.get("https://passport.jd.com/uc/login")
    time.sleep(10)


    goods_name = input('请输入你要查询的商品名: ')

    # 定位到搜索栏框
    driver.find_element_by_xpath('//input[@id="key"]').send_keys(goods_name)
    # 定位到按钮并点击
    driver.find_element_by_xpath('//button[@clstag="h|keycount|h|keycount|head|search_a"]').click()
    # 留时间给用户进行登录操作
    time.sleep(15)

    '''
    div[@class="p-name p-name-type-2"]  商品名称  /n
    div[@class="p-price"]   价格
    div[@class="p-commit"]   评价数
    div[@class="p-shop"]   店铺
    div[@class="p-icons"]  标签
    '''
    gname_list = []
    price_list = []
    commit_list = []
    shop_list = []
    icons_list = []

    get_all_data(driver, gname_list, price_list, commit_list, shop_list, icons_list)
    quit_driver(driver)

    dict1 = {
        '商品名称': gname_list,
        '价格': price_list,
        '评价数': commit_list,
        '店铺': shop_list,
        '标签': icons_list
    }

    df1 = pd.DataFrame(dict1)

    df1.to_csv(f'data/京东{goods_name}商品前10页信息.csv')
